<?php

namespace app\admin\service;

use app\admin\model\LoginLogModel;
use app\admin\model\OperationLogModel;

class LoginLogService extends BaseService
{
   /**
     * 获取登陆日志列表
     *
     * @Author tanruizheng
     * @DateTime 2021-07-12
     * @param array $where
     * @return void
     */
    public static function loginLog($params = [], $page = 1, $limit = 15){
        $where = [];
        //状态
        if(isset($params['status']) && $params['status'] !== ''){
            $where[] = ['status','=',$params['status']];
        }
        //用户名
        if(isset($params['username']) && $params['username'] !== ''){
            $where[] = ['username','=',$params['username']];
        }
        //登陆时间
        if(isset($params['logintime']) && $params['logintime']){
            $loginTime = explode('-',$params['logintime']);
            $startTime = strtotime($loginTime[0]);
            $endTIme = strtotime($loginTime[1]);
            $where[] = ['logintime','between',[$startTime,$endTIme]];
        }
        //列表
        $loginLog = LoginLogModel::where($where)->page($page,$limit)->select()->toArray();
        foreach ($loginLog as &$value) {
            //登录时间
            if($value['logintime']){
                $value['logintime'] = date('Y-m-d H:i:s',$value['logintime']);
            }
        }
        //总数
        $count = LoginLogModel::where($where)->count();
        return self::createList($loginLog,$count);
    }

    /**
     * 获取操作日志列表
     *
     * @Author tanruizheng
     * @DateTime 2021-07-12
     * @param array $where
     * @return void
     */
    public static function operationLog($params = [], $page = 1, $limit = 15){
        $where = [];
        //状态
        if(isset($params['status']) && $params['status'] !== ''){
            $where[] = ['status','=',$params['status']];
        }
        //时间
        if(isset($params['time']) && $params['time']){
            $loginTime = explode('-',$params['time']);
            $startTime = strtotime($loginTime[0]);
            $endTIme = strtotime($loginTime[1]);
            $where[] = ['time','between',[$startTime,$endTIme]];
        }
        //列表
        $list = OperationLogModel::where($where)->page($page,$limit)->select()->toArray();
        foreach ($list as &$value) {
            //登录时间
            if($value['time']){
                $value['time'] = date('Y-m-d H:i:s',$value['time']);
            }
        }
        //总数
        $count = OperationLogModel::where($where)->count();
        return self::createList($list,$count);
    }

    /**
     * 删除30天前的登陆日志
     *
     * @Author tanruizheng
     * @DateTime 2021-07-15
     * @return void
     */
    public static function deleteLoginLog(){
        $startTime = strtotime('-30 day');//30天前
        $res= LoginLogModel::where('logintime','<=',$startTime)->delete();
        if($res !== false){
            return self::createSuccess('删除成功');
        }
        return self::createFail('删除失败');
    }
    

    /**
     * 删除30天前的操作日志
     *
     * @Author tanruizheng
     * @DateTime 2021-07-15
     * @return void
     */
    public static function deleteOperationLog(){
        $startTime = strtotime('-30 day');//30天前
        $res= OperationLogModel::where('time','<=',$startTime)->delete();
        if($res !== false){
            return self::createSuccess('删除成功');
        }
        return self::createFail('删除失败');
    }
}